package vip.xiaonuo.device.modular.service.impl;

import cn.hutool.core.lang.Assert;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import vip.xiaonuo.device.modular.mapper.CabinetDoorMapper;
import vip.xiaonuo.device.modular.mapper.GoodsLedgerMapper;
import vip.xiaonuo.device.vo.CabinetScreenDoorVO;
import vip.xiaonuo.device.vo.CabinetScreenGoodsLedgerVO;
import vip.xiaonuo.device.vo.Result;

import javax.annotation.Resource;

/**
 * @author 墨塵
 * @since 2023/1/5
 */
@Service("deviceCabinetDoorService")
public class DeviceCabinetDoorServiceImpl extends DeviceBaseService {

    @Resource
    private CabinetDoorMapper cabinetDoorMapper;

    @Resource
    private GoodsLedgerMapper goodsLedgerMapper;

    @Override
    protected Integer getReqType() {
        return null;
    }

    @Override
    protected Boolean getLogFlag() {
        return false;
    }

    @Override
    protected Result handle(JSONObject param) {
        String cabinetDoorId = param.getString("cabinetDoorId");
        CabinetScreenDoorVO vo = cabinetDoorMapper.getCabinetScreenInfo(cabinetDoorId);
        Assert.notNull(vo, "柜门id不存在");
        String adminNames = this.getAdminName(vo.getId(), vo.getCabinetInfoId(), vo.getCabinetGroupId(), vo.getSeparatedPermission());
        vo.setAdminName(adminNames);
        CabinetScreenGoodsLedgerVO goodsLedger = goodsLedgerMapper.getOneByDoorId(cabinetDoorId);
        vo.setGoodsLedger(goodsLedger);
        return Result.ok(vo);
    }

    private String getAdminName(String doorId, String cabId, String groupId, Integer separatedPermission) {
        String result = null;
        if (separatedPermission == 1) {
            result = cabinetDoorMapper.getAdminForDoorId(doorId);
        } else {
            result = cabinetDoorMapper.getAdminForCabId(cabId);
            if (StringUtils.isBlank(result)) {
                result = cabinetDoorMapper.getAdminForGroupId(groupId);
            }
        }
        return result;
    }
}
